package cn.wonderland.house.controller;

import cn.wonderland.basic.anno.LogAnnotation;
import cn.wonderland.house.domain.ContractInformation;
import cn.wonderland.house.domain.RentingInformation;
import cn.wonderland.house.service.IContractInformationService;
import cn.wonderland.house.service.IHousingInformationService;
import cn.wonderland.house.domain.HousingInformation;
import cn.wonderland.house.query.HousingInformationQuery;
import cn.wonderland.basic.util.AjaxResult;
import cn.wonderland.basic.util.PageList;
import cn.wonderland.house.service.IRentingInformationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/housingInformation")
public class HousingInformationController {
    @Autowired
    public IHousingInformationService housingInformationService;

    @Autowired
    private IContractInformationService contractInformationService;

    @Autowired
    private IRentingInformationService rentingInformationService;


    /**
     * 保存和修改公用的
     *
     * @param housingInformation 传递的实体
     * @return AjaxResult转换结果
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody HousingInformation housingInformation) {
        try {
            if (housingInformation.getHId() != null){
                housingInformationService.update(housingInformation);
            }
            else{
                housingInformation.setHStatus(19);
                housingInformation.setHCreateTime(new Date());
                housingInformationService.add(housingInformation);
            }
            return AjaxResult.me().success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().fail();
        }
    }

    /**
     * 删除对象信息
     *
     * @param id
     * @return
     */
    @DeleteMapping(value = "/{id}")
    public AjaxResult delete(@PathVariable("id") Long id) {
        housingInformationService.delete(id);
        return AjaxResult.me().success();

    }

    //获取用户
    @GetMapping("/{id}")
    public HousingInformation get(@PathVariable("id") Long id) {
        return housingInformationService.findById(id);
    }


    /**
     * 查看所有的员工信息
     *
     * @return
     */
    @GetMapping()
    public List<HousingInformation> list() {

        return housingInformationService.findAll();
    }

    /**
     * 分页查询数据
     *
     * @param query 查询对象
     * @return PageList 分页对象
     */
    @PostMapping("/list")
    public PageList<HousingInformation> json(@RequestBody HousingInformationQuery query) {
        return housingInformationService.queryByPage(query);
    }


    /*
     * 批量删除信息
     * */
    @PatchMapping()
    public AjaxResult batchDelete(@RequestBody List<Long> ids) {
        housingInformationService.batchDelete(ids);
        return AjaxResult.me().success();
    }

    /**
     * 退租
     *
     * @param housingInformation 传递的实体
     * @return AjaxResult转换结果
     */
    @PutMapping("/offRent")
    public AjaxResult offRent(@RequestBody HousingInformation housingInformation) {

        Long id = housingInformation.getHId();
        ContractInformation coninfor = contractInformationService.findById(id);
        housingInformationService.updateHouseStateToOffRent(id);
        contractInformationService.updateStateToOff(id);


        Integer ctId = coninfor.getCtId();
        String rtRenter = coninfor.getCtPartyA();
        String hTitle = housingInformation.getHTitlex();
        BigDecimal hPrice = housingInformation.getHPrice();
        BigDecimal hArea = housingInformation.getHArea();

        RentingInformation rentingInformation = new RentingInformation(null, new Date(), hTitle, hPrice, hArea, ctId, rtRenter);

        rentingInformationService.add(rentingInformation);
        return AjaxResult.me().success();

    }


}
